//
// Created by cjh on 2021/5/22.
//

#include "Router.h"


Router router(R"(/)");


View* url::match(const char *str) const {
    std::cmatch match;
    int ret = std::regex_search(str, match, pattern);
    return ret ? view : nullptr;
}

View *Router::match(const char *str) const {
    if(urls.empty())
        return nullptr;

    std::cmatch match;
    int ret = std::regex_search(str, match, pattern);
    if(!ret)
        return nullptr;

    str += match.length();
    View*res=nullptr;
    for(auto& v:urls) {
        res = v->match(str);
        if(res) break;
    }
    return res;
}

Router::Router(std::initializer_list<_route*>&& li) {
    for(auto ptr : li) {
        urls.emplace_back(ptr);
    }
}

Router::~Router() {
    for(auto& url : urls) {
        delete url;
    }
}

Router &Router::operator+=(std::initializer_list<_route *> &&li) {
    for(auto ptr : li) {
        urls.emplace_back(ptr);
    }

    return *this;
}

